package com.itboye.sunsun.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.sunsun.R;
import com.itboye.sunsun.beans.OrderBean.InnerDetailBean;
import com.itboye.sunsun.utils.DoubleParse;
import com.itboye.sunsun.utils.XImageLoader;

public abstract class AbstractOrderAdapter extends BaseAdapter {

	protected Context context;
	protected List<InnerDetailBean> list;

	public AbstractOrderAdapter(Context context, List<InnerDetailBean> list) {
		this.context = context;
		this.list = list;

	}

	@Override
	public int getCount() {

		return list.size();
	}

	@Override
	public InnerDetailBean getItem(int arg0) {

		return list.get(arg0);
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	@Override
	public int getViewTypeCount() {

		return 6;
	}

	@Override
	public int getItemViewType(int position) {

		return list.get(position).getViewType();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		int type = getItemViewType(position);
		if (type == 0) {
			return crateGoodsView(position, convertView, parent);
		} else if (type == 1) {
			return crateOperateView(position, convertView, parent);
		} else if (type == 2) {
			return crateSendBottomView(position, convertView, parent);
		} else if (type == 3) {
			return crateReceiveView(position, convertView, parent);
		} else if (type == 4) {
			return crateCompleteView(position, convertView, parent);
		} else if (type == 5) {
			return crateCloseView(position, convertView, parent);
		}
		return null;
	}

	// 创建商品详情条目
	private View crateGoodsView(int position, View convertView, ViewGroup parent) {
		GoodsViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.item_order_name_picture_count, parent, false);
			holder = new GoodsViewHolder();

			holder.goods = (ImageView) convertView.findViewById(R.id.goods);
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.count = (TextView) convertView.findViewById(R.id.count);
			holder.guige = (TextView) convertView.findViewById(R.id.guige);
			holder.price = (TextView) convertView.findViewById(R.id.price);
			convertView.setTag(holder);
		} else {
			holder = (GoodsViewHolder) convertView.getTag();
		}
		holder.name.setText(list.get(position).getName());
		holder.guige.setText(list.get(position).getSkuDesc());
		holder.price.setText(list.get(position).getPrice());
		XImageLoader.load(list.get(position).getImgUrl(), holder.goods);
		holder.count.setText("x" + list.get(position).getCount());
		return convertView;
	}

	// 创建支付或者取消订单
	private View crateOperateView(int position, View convertView, ViewGroup parent) {
		PayOrCancelViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.item_order_pay_or_cancel, parent, false);
			holder = new PayOrCancelViewHolder();
			holder.pay = (TextView) convertView.findViewById(R.id.pay);
			holder.delete = (TextView) convertView.findViewById(R.id.delete);
			holder.price = (TextView) convertView.findViewById(R.id.price);
			convertView.setTag(holder);

			holder.delete.setText(onGetLeftTextviewText());
			holder.pay.setText(onGetRightTextviewText());

			holder.pay.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// DebugToast.show(v.getTag(), 1);
					onRightViewClick(v);
				}
			});
			holder.delete.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// deleteorder((String)v.getTag());
					onLeftViewClick(v);
				}
			});

		} else {
			holder = (PayOrCancelViewHolder) convertView.getTag();
		}

		InnerDetailBean bean = list.get(position);
		holder.pay.setTag(bean.getId());
		holder.delete.setTag(bean.getOrderCode());
		
		
		if(!bean.getDiscountRadio().equals("0.00"))
		{ 
		holder.price.setText(
				"共计" + bean.getCount() + "商品  合计￥" + String.format("%.2f", DoubleParse.parse(bean.getPrice(), 0))
						+ "(优惠￥" + String.format("%.2f", DoubleParse.parse(bean.getDiscountRadio(), 0)) + "含运费￥"
						+ String.format("%.2f", DoubleParse.parse(bean.getPostPrice(), 0)) + ")");
		}else
		{
			holder.price.setText(
					"共计" + bean.getCount() + "商品  合计￥" + String.format("%.2f", DoubleParse.parse(bean.getPrice(), 0))
							+ "(含运费￥"
							+ String.format("%.2f", DoubleParse.parse(bean.getPostPrice(), 0)) + ")");
		}
		return convertView;
	}

	// 创建待发货界面
	private View crateSendBottomView(int position, View convertView, ViewGroup parent) {
		SendViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.myorder_item_send, parent, false);
			holder = new SendViewHolder();

			holder.price = (TextView) convertView.findViewById(R.id.price);
			convertView.setTag(holder);

		} else {
			holder = (SendViewHolder) convertView.getTag();
		}

		InnerDetailBean bean = list.get(position);

		if(!bean.getDiscountRadio().equals("0.00"))
		{ 
		holder.price.setText(
				"共计" + bean.getCount() + "商品  合计￥" + String.format("%.2f", DoubleParse.parse(bean.getPrice(), 0))
						+ "(优惠￥" + String.format("%.2f", DoubleParse.parse(bean.getDiscountRadio(), 0)) + "含运费￥"
						+ String.format("%.2f", DoubleParse.parse(bean.getPostPrice(), 0)) + ")");
		}else
		{
			holder.price.setText(
					"共计" + bean.getCount() + "商品  合计￥" + String.format("%.2f", DoubleParse.parse(bean.getPrice(), 0))
							+ "(含运费￥"
							+ String.format("%.2f", DoubleParse.parse(bean.getPostPrice(), 0)) + ")");
		}
		return convertView;
	}

	// 创建待收货Item
	private View crateReceiveView(int position, View convertView, ViewGroup parent) {
		ReceiveHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.myorder_item_receive, parent, false);
			holder = new ReceiveHolder();

			holder.price = (TextView) convertView.findViewById(R.id.price);
			holder.pay = (TextView) convertView.findViewById(R.id.pay);
			holder.delete = (TextView) convertView.findViewById(R.id.delete);
			convertView.setTag(holder);

			holder.pay.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// DebugToast.show(v.getTag(), 1);
					onRightViewClick(v);
				}
			});
			holder.delete.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// deleteorder((String)v.getTag());
					onLeftViewClick(v);
				}
			});

		} else {
			holder = (ReceiveHolder) convertView.getTag();
		}

		InnerDetailBean bean = list.get(position);

		holder.pay.setTag(bean.getOrderCode());
		holder.delete.setTag(bean.getOrderCode());

		if(!bean.getDiscountRadio().equals("0.00"))
		{ 
		holder.price.setText(
				"共计" + bean.getCount() + "商品  合计￥" + String.format("%.2f", DoubleParse.parse(bean.getPrice(), 0))
						+ "(优惠￥" + String.format("%.2f", DoubleParse.parse(bean.getDiscountRadio(), 0)) + "含运费￥"
						+ String.format("%.2f", DoubleParse.parse(bean.getPostPrice(), 0)) + ")");
		}else
		{
			holder.price.setText(
					"共计" + bean.getCount() + "商品  合计￥" + String.format("%.2f", DoubleParse.parse(bean.getPrice(), 0))
							+ "(含运费￥"
							+ String.format("%.2f", DoubleParse.parse(bean.getPostPrice(), 0)) + ")");
		}
		return convertView;
	}

	// 创建已完成Item
	private View crateCompleteView(int position, View convertView, ViewGroup parent) {
		CompleteViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.myorder_item_complete, parent, false);
			holder = new CompleteViewHolder();

			holder.price = (TextView) convertView.findViewById(R.id.price);
			holder.delete = (TextView) convertView.findViewById(R.id.delete);
			convertView.setTag(holder);

		} else {
			holder = (CompleteViewHolder) convertView.getTag();
		}

		InnerDetailBean bean = list.get(position);

		if(!bean.getDiscountRadio().equals("0.00"))
		{ 
		holder.price.setText(
				"共计" + bean.getCount() + "商品  合计￥" + String.format("%.2f", DoubleParse.parse(bean.getPrice(), 0))
						+ "(优惠￥" + String.format("%.2f", DoubleParse.parse(bean.getDiscountRadio(), 0)) + "含运费￥"
						+ String.format("%.2f", DoubleParse.parse(bean.getPostPrice(), 0)) + ")");
		}else
		{
			holder.price.setText(
					"共计" + bean.getCount() + "商品  合计￥" + String.format("%.2f", DoubleParse.parse(bean.getPrice(), 0))
							+ "(含运费￥"
							+ String.format("%.2f", DoubleParse.parse(bean.getPostPrice(), 0)) + ")");
		}
		return convertView;
	}

	// 创建交易关闭Item
	private View crateCloseView(int position, View convertView, ViewGroup parent) {
		CompleteViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.myorder_item_close, parent, false);
			holder = new CompleteViewHolder();

			convertView.setTag(holder);

		} else {
			holder = (CompleteViewHolder) convertView.getTag();
		}

		return convertView;
	}

	public abstract String onGetLeftTextviewText();

	public abstract String onGetRightTextviewText();

	public abstract void onLeftViewClick(View v);

	public abstract void onRightViewClick(View v);

	final class GoodsViewHolder {

		ImageView goods;

		TextView name;
		TextView count;
		TextView guige;
		TextView price;

	}

	final class PayOrCancelViewHolder {

		TextView price;

		TextView delete;
		TextView pay;
	}

	final class SendViewHolder {

		TextView price;

	}

	final class ReceiveHolder {

		TextView price;
		TextView delete;
		TextView pay;

	}

	final class CompleteViewHolder {

		TextView price;

		TextView delete;
	}

	final class CloseViewHolder {

		TextView close;

	}
}
